package cn.zhagzheguo.xk.util;

import java.text.DecimalFormat;

public class NumberFormatUtil {

	/**
	 * 将number四舍五入到保留len位小数
	 * 
	 * @param number
	 * @param len
	 *            len>=0
	 * @return
	 */
	public static String format(String number, int len) {
		if (len < 0) {
			throw new RuntimeException("Wrong input length " + len + " in " + NumberFormatUtil.class);
		}
		return String.format("%." + len + "f", Double.parseDouble(number));
	}

	public static void main(String[] args) {
		String str = "81.666";
		long startTime = System.currentTimeMillis();
		DecimalFormat df = new DecimalFormat("#");
		System.out.println(df.format(Double.parseDouble(str)));
		long endTime = System.currentTimeMillis();
		long startTime2 = System.currentTimeMillis();
		System.out.println(String.format("%.0f", Double.parseDouble(str)));
		long endTime2 = System.currentTimeMillis();
		System.out.println("=================");
		System.out.println((endTime2 - startTime2) - (endTime - startTime));
	}
}
